package com.caesar.Set;

import org.junit.jupiter.api.Test;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

/**
 * @author caesar
 * @date 2025/7/16
 * @Description:
 * @apiNote
 */
public class TestSet {
    @Test
    public void test1() {
        //HashSet是Set的实现类，Set是Collection的子接口
        HashSet<String> set = new HashSet<>();
        set.add("hello");
        set.add("hello");
        set.add("world");
        set.add("world");
        set.add("java");
        System.out.println(set);
        //[world, java, hello]
    }

    @Test
    public void test2() {
        LinkedHashSet<String> set = new LinkedHashSet<>();
        set.add("hello");
        set.add("hello");
        set.add("world");
        set.add("world");
        set.add("java");
        System.out.println(set);
        //[hello, world, java]
    }

    @Test
    public void test3() {
        TreeSet<String> set = new TreeSet<>();
        set.add("hello");
        set.add("hello");
        set.add("world");
        set.add("world");
        set.add("java");
        set.add("chai");
        set.add("atguigu");
        System.out.println(set);
        //[atguigu, chai, hello, java, world]
        //按照字符串的字母的顺序
        //String实现类Comparable接口，所以字符串对象可以通过compareTo方法比较大小
    }

    @Test
    public void test4() {
        TreeSet<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(15);
        set.add(6);
        set.add(3);
        set.add(29);
        System.out.println(set);
        //[1, 3, 6, 15, 29]
        //Integer实现类Comparable接口，所以整数对象可以通过compareTo方法比较大小
    }

    @Test
    public void test5() {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student(1, "张三", 89, 23));
        set.add(new Student(2, "张三", 89, 26));
        set.add(new Student(3, "李四", 89, 24));
        set.add(new Student(4, "王五", 76, 25));
        //默认按照成绩排序

        //遍历集合
        for (Student s : set) {
            System.out.println(s);
        }
    }

    @Test
    public void test6() {
        HashSet<Student> set = new HashSet<>();
        set.add(new Student(1, "张三", 89, 23));
        set.add(new Student(1, "张三", 89, 23));
        set.add(new Student(2, "张三", 89, 26));
        set.add(new Student(3, "李四", 89, 24));
        set.add(new Student(4, "王五", 76, 25));

        //遍历集合
        for (Student s : set) {
            System.out.println(s);
        }
    }

    @Test
    public void test7() {
        //按照年龄排序
        //当Comparable的compareTo的规则不符合我们的排序要求，就要考虑Comparator接口
        Comparator c = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student s1 = (Student) o1;
                Student s2 = (Student) o2;
                int result = Integer.compare(s1.getAge(), s2.getAge());
                return result != 0 ? result : Integer.compare(s1.getId(), s2.getId());
            }
        };

        TreeSet<Student> set = new TreeSet<>(c);//告诉TreeSet用Comparator比较元素大小
        set.add(new Student(1, "张三", 89, 23));
        set.add(new Student(2, "张三", 89, 26));
        set.add(new Student(3, "李四", 89, 24));
        set.add(new Student(4, "王五", 76, 23));


        //遍历集合
        for (Student s : set) {
            System.out.println(s);
        }
    }

    @Test
    public void test8() {
        //按照年龄排序  降序
        //当Comparable的compareTo的规则不符合我们的排序要求，就要考虑Comparator接口
        Comparator c = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student s1 = (Student) o1;
                Student s2 = (Student) o2;
                int result = Integer.compare(s2.getAge(), s1.getAge());
                return result != 0 ? result : Integer.compare(s1.getId(), s2.getId());
            }
        };

        TreeSet<Student> set = new TreeSet<>(c);//告诉TreeSet用Comparator比较元素大小
        set.add(new Student(1, "张三", 89, 23));
        set.add(new Student(2, "张三", 89, 26));
        set.add(new Student(3, "李四", 89, 24));
        set.add(new Student(4, "王五", 76, 23));


        //遍历集合
        for (Student s : set) {
            System.out.println(s);
        }
    }
}
